package com.linran.webjsonrsp.controller;

import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.net.URL;

@RequestMapping("spider")
@RestController
public class SpiderController {

    @GetMapping("start")
    public void spider() throws IOException {
        //do something...
    }

    public static void main(String[] args) throws IOException {
        String path = "C:\\Users\\Admin\\Desktop\\表情包";
        String url = "https://ext.se.360.cn/webstore/category";
        Document doc = Jsoup.connect(url).get();
        Elements eles = doc.select(".applist .appwrap");
        for (Element ele : eles) {
            String img = ele.selectFirst("img").attr("src");
            String title = ele.selectFirst("h3").text();
            String intro = ele.selectFirst(".intro").text();
            System.out.printf("img:%s title:%s intro:%s %n", img, title, intro);
            FileUtils.copyURLToFile(new URL(img), new File(path, title + ".png"));
        }
    }
}
